package cn.buk.qms.performance;

import cn.buk.common.util.DateUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

@Aspect
public class DaoPerformance {

    private static final Logger logger = LoggerFactory.getLogger(DaoPerformance.class);

    @Around("execution(* cn.buk.qms.service.DetrService.*(..))")
    public Object watchSearchDetr(ProceedingJoinPoint jp) {
        Object obj = null;

        Date beginTime = DateUtil.getCurDateTime();

        try {
            obj = jp.proceed();

            long elapsed = DateUtil.getCurDateTime().getTime() - beginTime.getTime();
            if (elapsed >= 300) {
                logger.warn("Elapsed time is  {} ms", elapsed + ", " + jp.toString());
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        return obj;
    }
}
